package com.brave.file.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.UploadFileRequest;
import com.brave.base.BaseController;
import com.brave.base.RedisKit;
import com.brave.file.service.FileService;
import com.brave.user.dto.RedisUserInfo;
import com.brave.utils.AliOSSConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Api(value = "文件上传部分", tags = "文件上传", description = "文件上传")
@RestController
@RequestMapping("/v1/file")
public class FileUpdateController extends BaseController
{
    @Autowired
    private RedisKit redisKit;
    @Autowired
    private FileService fileService;
    @Autowired
    private OSS ossClientBuilder;
    
    
    
    
    @ApiOperation(value = "文件-上传")
    @PostMapping(value = "/uploadFile", headers = "content-type=multipart/form-data", produces = "application/json")
    public String uploadFile(@RequestHeader String token, @RequestParam MultipartFile file)
    {
        //上传文件记录
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用业务逻辑
        return fileService.uploadFile(file, redisUserInfo);
    }
    
    
    
    
    @ApiOperation(value = "上传至阿里云OSS")
    @GetMapping(value = "/uploadToAliOSS", produces = "application/json")
    public String uploadToAliOSS() throws Exception
    {

        File file = new File("D:" + File.separator + "baidu.png");
        InputStream is = new FileInputStream(file);
        ossClientBuilder.putObject(AliOSSConfig.ALI_OSS_BUCKET_NAME, file.getName(), is);
        is.close();
        ossClientBuilder.shutdown();
        return success();
    }
    
    
    
    
    
    
    
    
    
    
    
}